<template>
  <div class="header">
    <div class="homeIcon">
      <el-icon
        size="30px"
        @click="handleCollapse"
        :style="{ transform: isCollapse ? '' : 'rotate(180deg)' }"
        class="sy"
      >
        <expand />
      </el-icon>
      <span>首页</span>
    </div>
    <div class="user" @click="change">
      <img :src="userInfo.headImg" />
      <div class="userInfo" v-show="show">
        <div>{{ userInfo.name }}</div>
        <div @click="loginOut">退出登录</div>
      </div>
    </div>
    <!-- 音乐 -->
    <div class="music">
      <audio :src="mp3" controls="controls"></audio>
    </div>
    <!-- 文字 -->
    <div class="wz">你 走 过 的 地 方 只 剩 下 思 念 的 捱</div>
  </div>
</template>
<script setup>
import mp3 from '../audio/love.mp3'
import router from '../router/index'
import { defineProps, onMounted, reactive, ref } from 'vue'
import { getUserInfo } from '../api/index'
// 获取父组件参数
const { handleCollapse, isCollapse } = defineProps([
  'handleCollapse',
  'isCollapse',
])

// 显示与隐藏用户信息
const show = ref(false)
const change = () => {
  if (show.value) {
    show.value = false
  } else {
    show.value = true
  }
}

// 获取用户信息
const userInfo = reactive({
  name: '',
  headImg: '',
})
// 用户信息接口的调用
const getUserInfoData = async () => {
  const res = await getUserInfo()
  if (res.data?.name && res.data?.head_img) {
    userInfo.name = res.data.name
    userInfo.headImg = res.data.head_img
  }
}

onMounted(() => {
  getUserInfoData()
})

// 退出登录按钮
const loginOut = () => {
  router.push('/login'), localStorage.clear()
}
</script>
<style lang="less" scoped>
.music {
  width: 300px;
  height: 50px;
  position: absolute;
  top: 3px;
  right: 280px;
}

.wz {
  position: absolute;
  top: 20px;
  right: 567px;
  width: 300px;
  height: 20px;
  font-size: 12px;
  font-weight: 555;
  text-align: center;
  line-height: 20px;
}

.userInfo {
  z-index: 22;
  display: flex;
  flex-direction: column;
  position: absolute;
  right: 0;
  bottom: -77px;
  background-color: #fff;
  border: 5px;
  box-shadow: 0 4px 8px 0 rgb(7 17 27 / 10%);
  text-align: center;

  div:hover {
    color: #409eff;
  }

  div {
    padding: 10px;
  }
}

.header {
  position: relative;
  height: 100%;
  align-items: center;
  display: flex;
  justify-content: space-between;

  .homeIcon {
    display: flex;
    align-items: center;

    span {
      margin-left: 10px;
    }
  }

  .user {
    display: flex;
    justify-content: center;
    width: 80px;
    cursor: pointer;
    img {
      width: 80px;
      height: 80px;
      border-radius: 50%;
    }
  }
}
.sy {
  cursor: pointer;
}
</style>
